﻿using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using NewLife;

namespace PMS.WebApi.Utils
{
    public class JwtHelper
    {
        //私钥  
        public const string JwtSecret = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNAmD7RTE2drj6hf3oZjJpMPZUQ1Qjb5H3K3PNwIDAQABWelcomeToLeedarson";

        /// <summary>
        /// <summary>
        /// 生成JwtToken
        /// </summary>
        /// <param name="payload">不敏感的用户数据</param>
        /// <returns></returns>
        public static string SetJwtEncode(string username, int expiresMinutes, object data)
        {
            //格式如下
            var payload = new Dictionary<string, object>
            {
                { "iss", "LDS" },
                { "username", username },
                { "iat", DateTime.Now.ToString() },
                { "exp", DateTimeOffset.UtcNow.AddMinutes(expiresMinutes).ToUnixTimeSeconds() },
                { "sub", "JWT" },
                { "data", data },
                { "domain", "" },
                { "request_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                { "expire_time", DateTime.Now.AddMinutes(expiresMinutes).ToString("yyyy-MM-dd HH:mm:ss") }
            };
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
            IJsonSerializer serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
            var token = encoder.Encode(payload, JwtSecret);
            return token;
        }
        /// <summary>
        /// 根据jwtToken  获取实体
        /// </summary>
        /// <param name="token">jwtToken</param>
        /// <returns></returns>
        public static IDictionary<string, object> GetJwtDecode(string token)
        {
            if (token.IsNullOrEmpty())
                return null;
            IJsonSerializer serializer = new JsonNetSerializer();
            IDateTimeProvider provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
            IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);
            var dicInfo = decoder.DecodeToObject(token, JwtSecret, verify: true); //token为之前生成的字符串
            return dicInfo;
        }
    }
}
